package com.grand.ch09;

import java.util.*;

public class CollectTest {

    public static void baseMethod(){
        List<String> staff = new LinkedList<>();
        staff.add("Amy");
        staff.add("Bob");
        staff.add("Carl");
        Iterator iterator = staff.iterator();
        String first = iterator.next().toString();
        String second = iterator.next().toString();
        System.out.println(first);
        System.out.println(second);
        System.out.println(iterator.hasNext());
        iterator.remove();
        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());
        System.out.println(iterator.hasNext());
        staff.add("Mark");
        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());
    }

    public static void LinkedListTest(){

        List<String> a = new LinkedList<>();
        a.add("Amy");
        a.add("Carl");
        a.add("Erical");

        List<String> b = new LinkedList<>();
        b.add("Bob");
        b.add("Doug");
        b.add("France");
        b.add("Gloria");

        ListIterator<String> aIter = a.listIterator();
        ListIterator<String> bIter = b.listIterator();

        while(bIter.hasNext()){
            if(aIter.hasNext()){
                aIter.next();
            }
            aIter.add(bIter.next());
        }

        System.out.println(a);

        bIter = b.listIterator();
        while(bIter.hasNext()){
            bIter.next();
            if(bIter.hasNext()){
                bIter.next();
                bIter.remove();
            }
        }

        System.out.println(b);

        a.removeAll(b);
        System.out.println(a);

        aIter = a.listIterator();
        System.out.println(aIter.nextIndex());
        System.out.println(aIter.previousIndex());




    }

    public static void SetTest(){
        Set<String> words = new HashSet<>();
        long totalTime = 0;
        try(Scanner in = new Scanner(System.in)){
            while(in.hasNext()){
                String word = in.next();
                long callTime = System.currentTimeMillis();
                words.add(word);
                callTime = System.currentTimeMillis() - callTime;
                totalTime += callTime;
            }
        }

        Iterator<String> iter = words.iterator();
        for(int i=1;i<20&&iter.hasNext();i++){
            System.out.println(iter.next());
        }
        System.out.println("...");
        System.out.println(words.size());
    }

    public static void treeSetTest(){
        SortedSet<Item> parts = new TreeSet<>();
        parts.add(new Item("Toaster",1234));
        parts.add(new Item("Widget",456));
        parts.add(new Item("Moden",9912));
        System.out.println(parts);
        NavigableSet<Item> sortByDescription = new TreeSet<>(
                Comparator.comparing(Item::getDescription)
        );
        sortByDescription.addAll(parts);
        System.out.println(sortByDescription);
        System.out.println(parts.first());
        System.out.println(parts.last());
        System.out.println(((TreeSet<Item>) parts).higher(parts.first()));

    }

    public static void prioirtyQueueTest(){
        /*
        * 优先级队列同TreeSet一样需要保存实现了comparable接口的对象，也可以在构造器中提供comparator对象
        * 优先级是典型的任务调度，每一个任务都有一个优先级，任务以随机顺序添加到队列中，每当启动一个新任务时，
        * 都将优先级较高的任务从队列中删除（由于习惯上设置1为最高优先级，所以会将最小的元素删除）
        * */
        PriorityQueue<Item> pq = new PriorityQueue<>();
        pq.add(new Item("Tom",1234));
        pq.add(new Item("Grnad",6666));
        pq.add(new Item("garnd",8888));
        pq.add(new Item("Grand", 9999));

        System.out.println("Iterating over elements");
        for(Item item:pq){
            System.out.println(item.toString());
        }

        for (Item item: pq
             ) {
            System.out.println("Removing elements");
            while(!pq.isEmpty()){
                System.out.println(pq.remove());
            }
        }

    }

    public static void mapTest(){
        Map<String,Item> items = new HashMap<>();
        items.put("1001", new Item("10001",6666));
        items.put("1002", new Item("10002",7777));
        items.put("1003", new Item("10003",8888));

        System.out.println(items);

        items.remove("1003");

        System.out.println(items.get("1002"));
    }

    public static void updataMapTest(){
        String[] wordList = {"a","b","c","d","a","b","c","e"};
        HashMap<String,Integer> counts = new HashMap<>();
        for(String word : wordList){
            counts.put(word,counts.getOrDefault(word,0) + 1);
        }
        System.out.println(counts);
    }

    /*
    * LinkedHashMap保持了链表访问的顺序性
    * 它有LinkHashMap构造函数中第一个容量，第二个填装因子，第三个是否实现最近最少访问
    * 第三个为true是访问过的元素就会被添加到链表的末尾
    * */
    public static void LinkedHashMapTest(){
        Map<String,Object> map = new LinkedHashMap<>();
        for (int i = 0; i < 5; i++) {
            map.put(i+"", i);
        }
        Iterator<Map.Entry<String,Object>> iterator  = map.entrySet().iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next().getValue());
        }

        map.get("2");
        map.get("3");
        System.out.println("================split line=============");
        Iterator<Map.Entry<String,Object>> iterator1 = map.entrySet().iterator();
        while(iterator1.hasNext()){
            System.out.println(iterator1.next().getValue());
        }
    }


    public static void main(String[] args) {
        //LinkedListTest();
        //treeSetTest();
        //prioirtyQueueTest();
        //mapTest();
        //updataMapTest();
        //LinkedHashMapTest();
        LruCache<String,Object> cache = new LruCache<String, Object>(10);
        for (int i = 1; i <= 15; i++) {
            cache.put(i+"" , i);
        }
        cache.get("10");
        cache.get("9");
        Iterator<Map.Entry<String,Object>> itera = cache.entrySet().iterator();
        for(;itera.hasNext();){
            Map.Entry<String,Object> entry = itera.next();
            System.out.println("key="+entry.getKey()+",value"+entry.getValue());
        }

    }
}
